﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using QSHC.LibrarySL.MVVM;
using QSHC.QSchedule.Silverlight.Helpers;
using QSHC.QSchedule.Web.Models;
using QSHC.QSchedule.Web.Services;
using System.ServiceModel.DomainServices.Client;
using QSHC.QSchedule.Silverlight.Views.Windows;
//using QSHC.QSchedule.Silverlight.Views.Windows;

namespace QSHC.QSchedule.Silverlight.ViewModels
{
    public class HolidayViewModel : ViewModelBase
    {
        private QScheduleDomainContext _dc = new QScheduleDomainContext();

        #region Property Holidays
        private IEnumerable<Holiday> _holidays;
        public IEnumerable<Holiday> Holidays
        {
            get { return _holidays; }
            set
            {
                if (_holidays != value)
                {
                    _holidays = value;
                    OnPropertyChanged(() => Holidays);
                }
            }
        }
        #endregion

        #region Property SelectedHoliday
        private Holiday _selectedHoliday;
        public Holiday SelectedHoliday
        {
            get { return _selectedHoliday; }
            set
            {
                if (_selectedHoliday != value)
                {
                    _selectedHoliday = value;
                    OnPropertyChanged(() => SelectedHoliday);

                    DeleteCommand.OnCanExecuteChanged();
                }
            }
        }
        #endregion

        #region Property SelectedDate
        private DateTime _selectedDate;
        public DateTime SelectedDate
        {
            get { return _selectedDate; }
            set
            {
                if (_selectedDate != value)
                {
                    _selectedDate = value;
                    OnPropertyChanged(() => SelectedDate);

                    AddCommand.OnCanExecuteChanged();
                }
            }
        }
        #endregion

        #region Property Name
        private string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                if (_name != value)
                {
                    _name = value;
                    OnPropertyChanged(() => Name);

                    AddCommand.OnCanExecuteChanged();
                }
            }
        }
        #endregion

        #region Property Remark
        private string _remark;
        public string Remark
        {
            get { return _remark; }
            set
            {
                if (_remark != value)
                {
                    _remark = value;
                    OnPropertyChanged(() => Remark);
                }
            }
        }
        #endregion

        //Daeng
        #region Property IsBusy
        private bool _isBusy;
        public bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                if (_isBusy != value)
                {
                    _isBusy = value;
                    OnPropertyChanged(() => IsBusy);
                }
            }
        }

        #endregion

        #region AddCommand
        private ViewModelCommand _addCommand = null;
        public ViewModelCommand AddCommand
        {
            get
            {
                if (_addCommand == null)
                    _addCommand = new ViewModelCommand(p => Add(p), p => CanAdd);
                return _addCommand;
            }
        }

        private void Add(object obj)
        {
            AddHoliday(() =>
            {
                GetCurrentDateAllPatient(() =>
                {
                    var vm = new AppointmentListViewModel(SelectedDate, SelectedDate,null);
                    var win = new AppointmentListWindow();
                    win.appointmentList.DataContext = vm;

                    win.Show();
                    win.Unloaded += (sender, args) =>
                    {
                        
                    };
                });
            });
        }

        public bool CanAdd
        {
            get { return SelectedDate != null && !string.IsNullOrWhiteSpace(Name); }
        }
        #endregion

        #region DeleteCommand
        private ViewModelCommand _deleteCommand = null;
        public ViewModelCommand DeleteCommand
        {
            get
            {
                if (_deleteCommand == null)
                    _deleteCommand = new ViewModelCommand(p => Delete(p), p => CanDelete);
                return _deleteCommand;
            }
        }

        private void Delete(object obj)
        {
            var result = MessageBox.Show("คุณแน่ใจที่จะลบวันหยุดนี้", "คำเตือน", MessageBoxButton.OKCancel);

            if (result == MessageBoxResult.OK)
                DeleteHoliday();
        }

        public bool CanDelete
        {
            get { return SelectedHoliday != null; }
        }
        #endregion
                
        public HolidayViewModel()
        {
            if (!IsInDesignModeStatic)
            {
                Reset();
                LoadHolidays();
                SelectedDate = DateTime.Today;
            }
        }

        private void LoadHolidays()
        {
            var lo = _dc.Load(_dc.GetHolidaysQuery());
            lo.Completed += (s, e) =>
            {
                Holidays = lo.Entities;
            };
        }

        private void Reset()
        {
            Name = string.Empty;
            Remark = string.Empty;
        }

        private void AddHoliday(Action callback)
        {
            if (!VerifyHoliday())
            {
                return;
            }

            var remark = Remark.Trim();

            var holiday = new Holiday
            {
                HolidayDate = SelectedDate.Date,
                Name = Name,
                Remark = string.IsNullOrWhiteSpace(remark) ? null : remark,
            };

            var log = new LogHoliday
            {
                EmployeeCode = EmployeeHelper.LoggedEmployee,
                LogDateTime = DateTime.Now,
                Action = 1, // Create
                Holiday = holiday,
            };

            _dc.Holidays.Add(holiday);
            _dc.SubmitChanges().Completed += (s, e) =>
            {
                //MessageBox.Show("บันทึกแล้ว");

                Reset();
                LoadHolidays();
                callback.Invoke();
            };
        }

        private void DeleteHoliday()
        {
            SelectedHoliday.IsDeleted = true;

            var log = new LogHoliday
            {
                EmployeeCode = EmployeeHelper.LoggedEmployee,
                LogDateTime = DateTime.Now,
                Action = 4, // Delete
                Holiday = SelectedHoliday,
            };

            _dc.SubmitChanges().Completed += (s, e) =>
            {
                Reset();
                LoadHolidays();
            };
        }

        private bool VerifyHoliday()
        {
            if (Holidays.Where(p => p.HolidayDate.Date == SelectedDate.Date).Count() != 0)
            {
                MessageBox.Show("ไม่สามารถเพิ่มได้ เนื่องจากตรงกับวันหยุดแห่งชาติ");
                return false;
            }

            return true;
        }

        //Daeng
        private void GetCurrentDateAllPatient(Action callback)
        {
            IsBusy = true;
            var qry = from p in _dc.GetAppointmentsQuery()
                      where p.AppointmentDate >= SelectedDate && p.AppointmentDate <= SelectedDate
                      select p;

            var lo = _dc.Load(qry);
            lo.Completed += (s, e) =>
            {
                IsBusy = false;
                if (lo.Entities.Count() > 0)
                {
                    callback.Invoke();
                }
            };
        }
    }
}
